/******************************************************************************
 * Copyright (C) 2011 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.mq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.code.common.exception.UtilException;
import com.code.common.systeminit.EnviromentInfo;

/**
 * 消息监听器
 * 
 * @author 郑重
 * @since 1.0
 * @version 2010-12-1 郑重
 */
public class MessageReceiveListener implements MessageListener {
    
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageReceiveListener.class);
    
    /**
     * 接收消息
     * 
     * @param message 消息
     * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
     */
    public void onMessage(Message message) {
        try {
            System.out.println("接收消息---------");
            MessageData objMessageData = (MessageData) ((ObjectMessage) message).getObject();
            String strBeanName = JMSUtil.getBeanName(objMessageData.getMessageName());
            MessageExecuteInterface objMessageExecuteInterface =
                EnviromentInfo.getWebApplicationContext().getBean(strBeanName, MessageExecuteInterface.class);
            
            objMessageExecuteInterface.onMessage(objMessageData);
        } catch (JMSException e) {
            LOGGER.error("处理消息异常：" + message);
        } catch (Exception e) {
            LOGGER.error("处理消息异常：" + message, e);
            throw new UtilException("处理消息异常：" + message, e);
        }
    }
}